package com.example.blog.mapper;

import com.example.blog.entity.Article;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author ly
 * @since 2023-08-03
 */
public interface ArticleMapper extends BaseMapper<Article> {

    @Select("<script>" +
            "select a.*," +
            " b.id \"category.id\"," +
            " b.name \"category.name\"," +
            " b.sort \"category.sort\"," +
            " b.introduce \"category.introduce\"" +
            " from article a" +
            " join category b on a.categoryId=b.id" +
            "<where>" +
            "<if test='label!=null and label!=\"\"'>and label like concat('%',#{label},'%')</if>" +
            "<if test='title!=null and title!=\"\"'>and title like concat('%',#{title},'%')</if>" +
            "</where>" +
            " order by a.id desc" +
            " limit #{begin}, #{size}" +
            "</script>")
    List<Article> selectNews(@Param("begin") int begin, @Param("size") int size,
        @Param("label") String label,@Param("title")  String title);

    @Select("select a.*," +
            " b.id \"category.id\"," +
            " b.name \"category.name\"," +
            " b.sort \"category.sort\"," +
            " b.introduce \"category.introduce\"" +
            " from article a" +
            " join category b on a.categoryId=b.id" +
            " where a.id=#{id}")
    Article findByid(int id);

    @Select("select GROUP_CONCAT(label) labels from article")
    Map<String,Object> selectLabels();
}
